<?php

declare(strict_types=1);

namespace app\api\controller\auth;

use app\api\controller\Api;
use app\common\system\model\AuthRole;
use Symfony\Component\VarDumper\Cloner\Data;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\facade\Db;
use util\Format;

class Role extends Api
{
    public function index()
    {
        $where = [];
        $post = $this->request->param();
        if (isset($post['id']) && $post['id'] > 0) $where[] = ['id', '=', $post['id']];
        if (isset($post['keyword']) && $post['keyword'] != '') $where[] = ['title', 'like', "%{$post['keyword']}%"];

        list($cfg) = Format::pagination();
        $model = new AuthRole();
        $pg = $model->where($where)->order('sort asc,id desc')->paginate($cfg);
        $listData = $pg->getCollection();

        $result['list'] = $listData;
        $result['total'] = $pg->total();
        return success($result, '获取数据成功');
    }

    public function picker()
    {
        $model = new AuthRole();
        $list = $model->select();
        return success($list, '获取数据成功');
    }

    // 读取角色详情
    public function read()
    {
        $id = $this->request->param('id');
        if (empty($id)) return error([], '参数错误');
        try {
            $where = [];
            $where[] = ['id', '=', $id];
            $model = new AuthRole();
            $row = $model->withoutField('create_time,update_time,delete_time')->where($where)->find();
            return success($row, '获取数据成功');
        } catch (DataNotFoundException $e) {
            return error([], $e->getMessage());
        }
    }


    public function nodes()
    {
        $id = $this->request->param('id');
        if (empty($id)) return error([], '参数错误');
        
        $where = [];
        $where[] = ['id', '=', $id];
        $model = new AuthRole();
        $row = $model->where($where)->find();
        if (empty($row)) return error([], '角色不存在');


        if ($this->request->isPut()) {
            $post = $this->request->put();
            try {
                $row->save(['pc' => $post]);

                return success([], '保存成功');
            } catch(DbException $e) {
                return error([], $e->getMessage());
            }
            
        } else {
            try {
                return success($row->pc, '获取数据成功');
            } catch (DataNotFoundException $e) {
                return error([], $e->getMessage());
            }
        }
    }

    // 创建角色
    public function create()
    {
        $post = $this->request->post();
        // 创建的时候清除干扰的信息
        unset($post['id']);
        $validate = new \app\common\system\validate\AuthRoleValidate();
        if (!$validate->scene('create')->check($post)) return error([], $validate->getError());

        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        $model = new \app\common\system\model\AuthRole();
        $saveRs = $model->save($post);

        if ($saveRs) {
            return success([], '保存成功');
        } else {
            return error([], '保存失败');
        }
    }

    // 编辑角色权限
    public function save()
    {
        $id = $this->request->param('id');
        if (empty($id)) return error([], '参数错误');

        $post = $this->request->put();

        $validate = new \app\common\system\validate\AuthRoleValidate();
        if (!$validate->scene('edit')->check($post)) return error([], $validate->getError());

        $where = [];
        $where[] = ['id', '=', $id];
        $model = new \app\common\system\model\AuthRole();
        $row = $model->where($where)->find();

        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        $saveRs = $row->save($post);
        if ($saveRs) {
            return success([], '保存成功');
        } else {
            return error([], '保存失败');
        }
    }

    // 删除指定角色
    public function delete()
    {
        $id = $this->request->param('id');
        if (empty($id)) return error([], '参数错误');

        $where = [];
        $where[] = ['id', '=', $id];
        $model = new \app\common\system\model\AuthRole();
        $row = $model->where($where)->find();
        if (empty($row)) return error([], '角色不存在');

        $rs = $row->delete();
        if ($rs) {
            return success([], '删除成功');
        } else {
            return error([], '删除失败');
        }
    }
}
